﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ABCLogistic_System_V2._0.DA;
using System.Data.SqlClient;

namespace ABCLogistic_System_V2._0.GUI
{
    public partial class frmQTHT_Nguoidung : Form
    {
        // khai bao bien toan cuc
        ABCLogisticsEntities context = new ABCLogisticsEntities();
        NHANVIEN nhanvien = new NHANVIEN();
        TAIKHOAN taikhoan = new TAIKHOAN();
        string strgioitinh;
        string  strManhanvien;
        bool bedit;
        bool bCheckSave;

        public delegate void LoadNhanvien(bool checkok);
        public event LoadNhanvien ok;

        public frmQTHT_Nguoidung(string pManhanvien,bool bEdit)
        {
            strManhanvien = pManhanvien;
            bedit = bEdit;
            InitializeComponent();
        }

        public frmQTHT_Nguoidung()
        {
            InitializeComponent();
        }

        #region Cac ham kiem tra va luu
        /// <summary>
        /// An cac button va text box khi xem thong tin
        /// </summary>
        public void Enable(bool bEnable)
        {
            btndoimatkhau.Enabled = bEnable;
            btnluu.Enabled = bEnable;
            txtdiachi.Enabled = bEnable;
            txtEmail.Enabled = bEnable;
            txtFax.Enabled = bEnable;
            txthovatendem.Enabled = bEnable;
            txtmanv.Enabled = bEnable;
            txtsodienthoai.Enabled = bEnable;
            txtten.Enabled = bEnable;
            txttenkhac.Enabled = bEnable;
            radnam.Enabled = bEnable;
            radNu.Enabled = bEnable;
            dbngaysinh.Enabled = bEnable;
            dbngayvaocongty.Enabled = bEnable;
            txtCMND.Enabled = bEnable;
            cbxnhom.Enabled = bEnable;
        }

        /// <summary>
        /// set gia tri la null cho cac textbox va radiobutton
        /// </summary>
        public void SetValueInText()
        {
            txtmanv.Text = "";
            txtCMND.Text = "_________";
            txthovatendem.Text = "";
            txtten.Text = "";
            txttenkhac.Text = "";
            radnam.Checked = false;
            radNu.Checked = false;
            txtdiachi.Text = "";
            txtsodienthoai.Text = "";
            txtFax.Text = "";
            txtEmail.Text = "";
            txtmatkhau.Text = "logistic";
            txtCMND.Text = "";
            cbxnhom.Text = "";
            lblchange.Text = "Thêm người dùng";
            this.Text = "Thêm người dùng";
            btnchinhsua.Enabled = false;
            txtmanv.Text = "";
        }

        /// <summary>
        /// kiem tra co noi dung ben trong textbox va radiobutton chua
        /// </summary>
        /// <returns></returns>
        public int CheckValueInText()
        {
            if (txtCMND.Text != "" || txtdiachi.Text != "" || txtEmail.Text != "" || txtFax.Text != "" || txthovatendem.Text != ""
                || txtsodienthoai.Text != "" || txtten.Text != "" || txttenkhac.Text != "" || (((radnam.Checked = false) && (radNu.Checked = true)) || ((radnam.Checked = true) && (radNu.Checked = false))))
            {
                return 1;
            }
            else
            {
                if (txtCMND.Text != "" && txtdiachi.Text != "" && txthovatendem.Text != "" && txtsodienthoai.Text != "" && txtten.Text != ""
                        && (((radnam.Checked = false) && (radNu.Checked = true)) || ((radnam.Checked = true) && (radNu.Checked = false))))
                {
                    return 2;
                }
                else { return 3; }
            }
            
        }

        /// <summary>
        /// Ham luu thong tin nguoi dung xuong database
        /// </summary>
        public void SaveInfo()
        {
            // luu xuong database
            string strmanhanvien = txtmanv.Text;
            string strhovaten=txthovatendem.Text + " " + txtten.Text;
            strmanhanvien = "";
            
            if (bCheckSave == true)
            {
                if (txtCMND.Text != null && txtdiachi.Text != "" && txthovatendem.Text != "" && txtsodienthoai.Text != "" && txtten.Text != ""
                       && (((radnam.Checked == false) && (radNu.Checked == true)) || ((radnam.Checked == true) && (radNu.Checked == false))))
                {
                    int? returnvalues1 = context.sp_NHANVIEN_Update(txtmanv.Text, Int32.Parse(txtCMND.Text), cbxnhom.SelectedValue.ToString(), txthovatendem.Text, txtten.Text,
                       txthovatendem.Text + " " + txtten.Text, txttenkhac.Text, strgioitinh, txtdiachi.Text, dbngaysinh.Value, Int32.Parse(txtsodienthoai.Text),
                        txtFax.Text, txtEmail.Text, cbxnhom.Text, txtmatkhau.Text, dbngayvaocongty.Value).SingleOrDefault();
                    if (returnvalues1 == 1)
                    {
                        DialogResult result = MessageBox.Show("Lưu thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        if (result == DialogResult.OK)
                        {
                            if (ok != null)
                            {
                                this.ok(true);
                            }
                            // xet lai cac gia tri trong textbox
                            SetValueInText();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Không thể lưu!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("Bạn chưa nhập đủ thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                if (txtCMND.Text != null && txtdiachi.Text != "" && txthovatendem.Text != "" && txtsodienthoai.Text != "" && txtten.Text != ""
                       && (((radnam.Checked == false) && (radNu.Checked == true)) || ((radnam.Checked == true) && (radNu.Checked == false))))
                {
                    int? returnvalues = context.sp_NHANVIEN_Insert(strmanhanvien, Int32.Parse(txtCMND.Text), cbxnhom.SelectedValue.ToString(), txthovatendem.Text, txtten.Text,
                  strhovaten, txttenkhac.Text, strgioitinh, txtdiachi.Text, dbngaysinh.Value, Int32.Parse(txtsodienthoai.Text),
                    txtFax.Text, txtEmail.Text, cbxnhom.Text, txtmatkhau.Text, dbngayvaocongty.Value).SingleOrDefault();

                    if (returnvalues == 1)
                    {
                        DialogResult result = MessageBox.Show("Lưu thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        if (result == DialogResult.OK)
                        {
                            if (ok != null)
                            {
                                this.ok(true);
                            }
                            // xet lai cac gia tri trong textbox
                            SetValueInText();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Không thể lưu!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("Bạn chưa nhập đủ thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }
                
        }

        #endregion

        
        private void frmQTHT_Nguoidung_Load(object sender, EventArgs e)
        {
            var nhomnguoidung = from cat in context.NHOMNGUOIDUNGs
                           select cat;
            cbxnhom.DataSource = nhomnguoidung.ToList<NHOMNGUOIDUNG>();
            cbxnhom.DisplayMember = "TENNHOM";
            cbxnhom.ValueMember = "MANHOM";

            if (bedit == true)
            {
                nhanvien = (from p in context.NHANVIENs
                            where p.MANV ==strManhanvien
                            select p).FirstOrDefault<NHANVIEN>();
                Enable(false);
                //load data len form
                txtmanv.Text = nhanvien.MANV;
                txtCMND.Text = nhanvien.SOCMND.ToString();
                txthovatendem.Text = nhanvien.HOVATENLOT;
                txtten.Text = nhanvien.TEN;
                txttenkhac.Text = nhanvien.TENKHAC;
                if (nhanvien.GIOITINH.ToString() == radNu.Text)
                {
                    radNu.Checked = true;
                }
                if (nhanvien.GIOITINH.ToString() == radnam.Text)
                {
                    radnam.Checked = true;
                }
                txtdiachi.Text = nhanvien.DIACHI;
                dbngaysinh.Value = nhanvien.NGAYSINH;
                txtsodienthoai.Text = nhanvien.SODIENTHOAI.ToString();
                txtFax.Text = nhanvien.FAX.ToString();
                txtEmail.Text = nhanvien.EMAIL;
                cbxnhom.Text = nhanvien.NHOM;
                dbngayvaocongty.Value = nhanvien.NGAYVAOCONGTY.Value;

                lblCMNN.Text = "  ";
                lblCMNN.Image = global::ABCLogistic_System_V2._0.Properties.Resources.Accept_icon;
            }
        }

        /// <summary>
        /// xu ly cua cac button luu, chinh sua,them moi
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 

        // gan chuoi khi check vao checkbox nam
        private void radnam_CheckedChanged(object sender, EventArgs e)
        {
            strgioitinh = radnam.Text;
        }
        // gan chuoi khi check vao checkbox nu
        private void radNu_CheckedChanged(object sender, EventArgs e)
        {
            strgioitinh = radNu.Text;
        }
        /// <summary>
        /// Xu ly khi them nguoi dung
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnthemnguoidung_Click(object sender, EventArgs e)
        {
            if (CheckValueInText() == 1)
            {
                DialogResult result = MessageBox.Show("Bạn có muốn lưu không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (result == DialogResult.No)
                {
                    SetValueInText();
                    Enable(true);
                }
                else SaveInfo();
            }
            else
            {
                SetValueInText();
                
            }
        }

        // xu ly button luu
        private void btnluu_Click(object sender, EventArgs e)
        {
            SaveInfo();
        }

        private void btnchinhsua_Click(object sender, EventArgs e)
        {
            bCheckSave = true;
            Enable(true);
        }
        /// <summary>
        /// chi cho nhap so vao
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtOnlyNumberKeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true; 
        }


        /// <summary>
        /// Xet CMNN nhap vao co dung khong
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtCMNN_TextChanged(object sender, EventArgs e)
        {
            string strCMNN = txtCMND.Text;

            if (strCMNN.Length == 9)
            {
                int iCMNN;
                Int32.TryParse(txtCMND.Text.ToString(), out iCMNN);
                NHANVIEN obj = context.NHANVIENs.Where(p => p.SOCMND == iCMNN).FirstOrDefault();
                if (obj == null)
                {
                    lblCMNN.Text = "  ";
                    lblCMNN.Image = global::ABCLogistic_System_V2._0.Properties.Resources.Accept_icon;
                }
                else
                {
                    lblCMNN.Text = "  ";
                    lblCMNN.Image = global::ABCLogistic_System_V2._0.Properties.Resources.X_icon;
                }
            }
            else
            {
                lblCMNN.Text = "(*)";
                lblCMNN.Image= null;
            }
        }

        private void btndoimatkhau_Click(object sender, EventArgs e)
        {
            frmQTHT_Doimatkhau doimatkhau = new frmQTHT_Doimatkhau();
            doimatkhau.ShowDialog();
        }

    }
}
